<!DOCTYPE html>
<html lang="en" style="transition: background-color 0.15s ease 0s;">
<head>
    {% load static %}
    <meta charset="UTF-8">
    <title>项目列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{% static '/css/layui.css' %}">
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <div class="layui-header header header-demo" style="background-color: #393D49">
        <div class="layui-main">
            <ul class="layui-nav">
                <button style="background-color: #393D49;margin-left: -20px;" type="button"
                        class="layui-btn layui-btn-normal"><i
                        class="layui-icon"></i></button>
                <li class="layui-nav-item"><a href="{%url 'TestPlatform_User:Home'%}"><h1>TestPlatform</h1></a></li>
                <li class="layui-nav-item layui-this">
                    <a href="{%url 'TestPlatform_User:Project_List'%}">项目列表</a>
                <li class="layui-nav-item">
                    <a href="{%url 'TestPlatform_User:Interface_List'%}">接口列表</a>
                </li>
                <li class="layui-nav-item">
                    <a href="{%url 'TestPlatform_User:Interface_Perform'%}">快速执行</a>
                </li>
                <li class="layui-nav-item"><a href="">测试报告</a></li>
                <li class="layui-nav-item"><a href="">邮件配置</a></li>
                <li class="layui-nav-item" style="float: right">
                    <a href="javascript:;">{{ user.get_username }}</a>
                    <dl class="layui-nav-child">
                        <dd><a href="{%url 'TestPlatform_User:Logout'%}"><i
                                class="layui-icon">&#xe64d; </i>登出</a></dd>
                    </dl>
                </li>
                <li style="margin:12px;float: right">
                    <div style="margin-left: 30px;">
                        <input type="hidden" name="color" value="" id="test-all-input">
                        <div id="test-all"></div>
                    </div>
                </li>
            </ul>
        </div>
    </div>
    <!--<div style="margin-left: 200px;">-->
    <div style="padding: 20px; background-color: #F2F2F2;">
        <div class=" demoTable " style="margin-top: 40px">
            <button type="button" class="layui-btn layui-btn-primary" id="re" data-type="re"
                    style="float: left" onclick="re()"><i class="layui-icon">&#xe65c;</i>返回
            </button>
            <button type="button" class="layui-btn layui-btn-danger" id="batchDelete" data-type="getCheckData"
                    style="float: right"><i class="layui-icon">&#xe640;</i>批量删除
            </button>
            <button id="add_project" class="layui-btn" type="button" data-method="notice" style="float:right; margin-right: 10px">新 增
            </button>
            <div class="demoTable" style="margin-top: -38px; margin-left: 140px">
                <div class="layui-inline">
                    <input type="text" class="layui-input" name="id" placeholder="可根据id进行搜索"
                           title="可根据id进行搜索" id="demoReload" autocomplete="off">
                </div>
                <button class="layui-btn" data-type="reload" style="margin-left: 10px;">搜 索</button>
            </div>
        </div>


        <!--</form>-->

        <table class="layui-hide" id="table_data" lay-filter="table_data"></table>
        <script type="text/html" id="toolbar">
            <a class="layui-btn layui-btn-xs" lay-event="add"
               href="{%url 'TestPlatform_User:Project_Info' %}">添加</a>
            <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="perform"
            >修改</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        </script>
    </div>
</div>

{% load static %}
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script src="{% static '/js/layui.js' %}" charset="utf-8"></script>
<script type="text/javascript">
    layui.use('element', function () {
        var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
        //监听导航点击
        element.on('nav(table_data)', function (elem) {
            //console.log(elem)
            layer.msg(elem.text());
        });
    });
    layui.use('table', function () {
        var table = layui.table;
    });

    layui.use(['layer', "jquery"], function () {
        var
            layer = layui.layer //弹层
            , $ = layui.jquery;
        $('#add_project').on("click", function () {
            layer.open({
                title: '新增项目',
                type: 2,
                skin: 'layui-layer-rim', //加上边框
                area: ['460px', '550px'], //宽高
                fix: true,
                // btn: ['确认提交', '取消'],
                shadeClose: true,//点击旁边地方自动关闭
                content: "../add_project",
            });

            // layer.close(layer.index);

            // layer.close(layer.index);
        })
    });
    //----------------------------------------------------------------------
    layui.use('table', function () {
        var table = layui.table;
        table.on('edit(table_data)', function (obj) {
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
            layer.confirm('确定将信息修改为：' + value, {
                btn: ['确定', '取消']
            }, function (index) {
                $.ajax({
                    type: 'post',
                    url: "{%url 'TestPlatform_User:Update_Data_Pj'%}",
                    data: {'data_id': data.id, 'field': field, 'value': value},
                    traditional: true,
                    async: false,
                    success: function (result) {
                    },
                    error: function (result) {
                    }
                });
                // window.location.reload();
                layer.close(index);
            }, function () {
                window.location.reload();
                layer.close(index);
            })
        });

        table.render({
            elem: '#table_data'	//表单ID
            , url: "{%url 'Api:get_pj_data'%}"	//数据接口
            , page: true			//分页设置
            , limit: 20		//默认每一页显示数量
            , limits: [10, 20, 30, 40, 50]		//可选择每页显示的数量
            , toolbar: true
            , cols: [[			//表格列
                {type: 'checkbox', sort: false}
                , {field: 'pj_id', title: '项目编号', align: 'center', sort: true, edit: 'int'}
                , {field: 'pj_name', title: '项目名称', align: 'center', sort: false, edit: 'text'}
                , {field: 'pj_pname', title: '项目经理', align: 'center', sort: false, edit: 'text'}
                , {field: 'pj_pn', title: '产品经理', align: 'center', sort: false, edit: 'text'}
                , {field: 'pj_tname', title: '测试人员', align: 'center', sort: false, edit: 'text'}
                , {field: 'pj_state', title: '状态', align: 'center', sort: true, edit: 'text'}
                , {field: 'dobar', title: '操作', align: 'center', sort: false, toolbar: '#toolbar'}
            ]]
            , response: {
                statusCode: 200
            }
            , id: 'testReload'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                var result;
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                } else {
                    result = res.data.slice(0, this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        });
        var $ = layui.$, active = {
            reload: function () {
                var demoReload = $('#demoReload');
                if (demoReload.val() === "") {
                    location.reload();
                } else {
                    //执行重载
                    table.reload('testReload', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                            id: demoReload.val(),
                        }
                    });
                }
            },
            getCheckData: function () { //获取选中数据
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data;
                var arr = JSON.stringify(data);
                if (data.length === 0) {
                    layer.msg('请选择需要删除的数据！')
                } else {
                    layer.confirm('是否删除选中的' + data.length + '条数据？', function (index) {
                        $.ajax({
                            type: 'post',
                            url: "{%url 'TestPlatform_User:Batch_Delete_Pj'%}",
                            data: {'arr': arr},
                            traditional: true,
                            async: false,
                        });
                        window.location.reload();
                    });
                }
            }
        };
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });

    function re() {
        window.history.back(-1)
    }

    layui.use('form', function () {
        var form = layui.form;
        //刷新界面 所有元素
        form.render();
        // 只刷新radio元素
        // form.render('radio');
    });
    layui.use('table', function () {
        var table = layui.table;
        //监听表格复选框选择
        table.on('checkbox(table_data)', function (obj) {
            console.log(obj)
        });
        //监听工具条 删除一条数据
        table.on('tool(table_data)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        type: 'post',
                        url: "{%url 'TestPlatform_User:Delete_Pj'%}",
                        data: {'data_id': data.id},
                        traditional: true,
                        async: false,
                        success: function (result) {
                        },
                        error: function (result) {
                        }
                    });
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'add') {
                $.ajax({
                    type: 'POST',
                    url: "{%url 'Api:project_data' %}",
                    data: {'data_id': data.id},
                    traditional: true,
                    async: false,
                    success: function (result) {
                    },
                    error: function (result) {
                    }
                });
                $.ajax({
                    type: 'POST',
                    url: "{%url 'TestPlatform_User:Project_Info'%}",
                    data: {'pj_name': data.pj_name},
                    traditional: true,
                    async: false
                });
                var storage = window.sessionStorage;
                storage['id'] = data.id;
            } else if (obj.event === 'perform') {
                layer.msg('双击单元格试试！')
            }
        });
    });

</script>
</body>
</html>